import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpReceive implements Runnable {
    private byte[] bytes = new byte[1024];
    private DatagramSocket socket;
    private String from;

    public UdpReceive(int port, String from) throws SocketException {
        this.socket = new DatagramSocket(port);
        this.from = from;
    }

    @Override
    public void run() {
        while (true) {
            DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
            try {
                this.socket.receive(datagramPacket);
                System.out.println(from + ":" + new String(datagramPacket.getData(), 0, datagramPacket.getLength()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
